<?php
class sample_detail_file_model extends MY_Model {
	var $model = 'sample_detail_file';
	
	function GetJSON($as_pk_master)
	{
		$ls_sql	= "	SELECT	*
					FROM	sample_detail_file
					WHERE	pk_master = '".$as_pk_master."' 
					ORDER BY ".(post('sort')=='' ? 'label' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		$arr_response	= $this->paging($ls_sql);
		for($i=0,$max_i=count($arr_response['rows']); $i<$max_i; $i++){
			$row	= $arr_response['rows'][$i];
			$row['filesize']	= stringFileSize($row['filesize']);
			
			$path_file	= APPPATH . '/../files/' . $row['filename'];
			if(in_array($row['ext'],array('jpg', 'jpeg', 'png', 'bmp', 'gif')) && file_exists($path_file)){
				$fileinfo = @getimagesize($path_file);
				$row['width'] = $fileinfo[0]; $row['height'] = $fileinfo[1];
			}
			$arr_response['rows'][$i] = $row;
		}
		
		return json_encode( $arr_response );
	}
	
	function GetDetailByID($as_pk_master)
	{
		$ls_sql	= "	SELECT	*
					FROM	sample_detail_file
					WHERE	pk_master = '".$as_pk_master."' 
					ORDER BY label";
		
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function Add($arr_data){
		$this->db->insert('sample_detail_file', $arr_data);
		
		$this->AuditTrail(	'sample_detail_file',// Table
							'Create',			// Action
							''					// Primary Key
							);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('sample_detail_file', $arr_data, $arr_rule);
		
		$this->AuditTrail(	'sample_detail_file',	// Table
							'Update',				// Action
							json_encode($arr_rule)	// Primary Key
							);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('sample_detail_file', array('ref_key' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('sample_detail_file', array('ref_key' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('sample_detail_file', $arr_rule);
			
			$this->AuditTrail(	'sample_detail_file',	// Table
								'Delete',				// Action
								json_encode($arr_rule)	// Primary Key
								);
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>